//
//  WKNavigationController.m
//  Wanka
//
//  Created by 郭晓东 on 2017/9/13.
//  Copyright © 2017年 郭晓东. All rights reserved.
//

#import "WKNavigationController.h"

#import "UIBarButtonItem+Extension.h"

@interface WKNavigationController () <UINavigationControllerDelegate,UIGestureRecognizerDelegate>

@end

@implementation WKNavigationController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(instancetype)initWithRootViewController:(UIViewController *)rootViewController
{
    self = [super initWithRootViewController:rootViewController];
    if (self) {
        rootViewController.extendedLayoutIncludesOpaqueBars = YES;
    }
    return self;
}

/**
 *  当导航控制器的view创建完毕就调用
 */
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.delegate = self;
    self.extendedLayoutIncludesOpaqueBars = NO;

    __weak typeof (self)weakSelf = self;
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        
        self.interactivePopGestureRecognizer.delegate = weakSelf;
    }
    
    
    //删除系统黑线
//    for (UIView *vv in [self.navigationBar.subviews firstObject].subviews) {
//        if ([vv isKindOfClass:[UIImageView class]]) {
//            vv.hidden = YES;
            //替换黑线
//            UIView *line = [[UIView alloc] initWithFrame:(CGRect){0,0,vv.bounds.size}];
//            line.backgroundColor = UIColorFromHex(0xe0e0e0);
//            [vv addSubview:line];
//        }
//    }
    
}

/**
 *  当第一次使用这个类的时候调用1次
 */
+ (void)initialize
{
    
}

/**
 *  能拦截所有push进来的子控制器
 */
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (viewController == nil) return;
    
    //解决 滑动过程 pushViewController激活滑动手势会导致crash
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.interactivePopGestureRecognizer.enabled = NO;
    }
    
    if (self.viewControllers.count > 0) { // 如果现在push的不是栈底控制器(最先push进来的那个控制器)
        viewController.hidesBottomBarWhenPushed = NO;
        // 设置导航栏按钮
        viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImageName:@"navigation_back" highImageName:@"navigation_back" target:viewController action:@selector(back)];
    }
    else
    {
        
    }
    [super pushViewController:viewController animated:animated];
}

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    
}

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (viewController == self.viewControllers[0]) { // 是根控制器
        self.interactivePopGestureRecognizer.delegate = nil;
        
        if ([navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
            navigationController.interactivePopGestureRecognizer.enabled = NO;
        }
    }else{ // 非根控制器
        self.interactivePopGestureRecognizer.delegate = (id <UIGestureRecognizerDelegate>)viewController;
        
        if ([navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
            navigationController.interactivePopGestureRecognizer.enabled = YES;
        }
    }
}


@end
